package com.tdh.service.impl;

import com.tdh.dao.UserDao;
import com.tdh.pojo.TUser;
import com.tdh.service.UserService;
import com.tdh.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public List<TUser> selectLoginUser(String yhid, String yhkl) {
        return userDao.selectByIdAndPwd(yhid, yhkl);
    }

    @Override
    public List<TUser> selectUserTableLimit (String user, String depart, int startRow, int limitNum) {

        List<TUser> list = userDao.selectUserTableLimit(user, depart, startRow, limitNum);

        TsBzdmServiceImpl tsBzdmService = new TsBzdmServiceImpl();
        System.out.println(tsBzdmService.translateSex("09_00003-1"));

        //转换数据格式
        for(TUser tUser: list) {
            tUser.setYhbm(DepartServiceImpl.translateDepart(tUser.getYhbm()));
            //tUser.setYhxb(tsBzdmService.translateSex(tUser.getYhxb()));
            tUser.setSfjy(tUser.getSfjy().equals("0") ? "否" : "是");
        }
        return list;
    }

    @Override
    public String countRows(String user, String depart) {
        return userDao.countRows(user, depart);
    }

    /**
     * 获取排序号最大值 + 1
     * @return int
     */
    @Override
    public int queryPxh() {
        return userDao.queryPxh() + 1;
    }

    @Override
    public void insertUser(TUser user) {
        userDao.insertUser(setUserInDB(user));
    }

    @Override
    public void updateUser(TUser user) {
         userDao.updateUser(setUserInDB(user));
    }

    @Override
    public void deleteUser(String yhid) {
        userDao.deleteUser(yhid);
    }

    @Override
    public TUser queryById(String yhid) {
        TUser user = userDao.selectUserById(yhid);

        //更改出生日期格式
        if(user != null) {
            user.setCsrq(StringUtil.setCsrqInHtml(user.getCsrq()));
        }
        return user;
    }

    public TUser setUserInDB(TUser user) {
        user.setYhxb(StringUtil.trimString(user.getYhxb()));
        user.setCsrq(StringUtil.setCsrqInDB(user.getCsrq()));
        return user;
    }
}
